﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BLL;
using System.Data;
using System.Web.Services;
using System.Web.Script.Serialization;

namespace Lanworks
{
    public partial class Customer : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                #region Get All City

                BCity bCity = new BCity();
                DataSet dsCity = bCity.GetAllCity();
                ddlCityzen.DataSource = dsCity;
                ddlCityzen.DataTextField = "CityName";
                ddlCityzen.DataValueField = "Id";
                ddlCityzen.DataBind();
                #endregion

                #region Get Branch

                BBranch bBranch = new BBranch();
                DataSet dsBranch = bBranch.GetAllBranch();
                ddlBranch.DataSource = dsBranch;
                ddlBranch.DataValueField = "Id";
                ddlBranch.DataTextField = "BranchName";
                ddlBranch.DataBind();
                #endregion

                #region Get Education Level
                BEducation bEducation = new BEducation();
                DataSet dsEducation = bEducation.GetAllEducation();
                ddlEducation.DataSource = dsEducation;
                ddlEducation.DataValueField = "Id";
                ddlEducation.DataTextField = "EduName";
                ddlEducation.DataBind();
                //ddlEducation.Items.Add(new ListItem("--- Other --- ","0"));
                #endregion

            }
        }

        [WebMethod]
        public static string InsertCustomer(string sBranch, string sFullName, int sNRIC, string sDateBirth, int sGender, int sCity, int sEducation, string sOtherEdu)
        {
            string sResult = string.Empty;
            try
            {
                BCustomer bBranch = new BCustomer();
                BCustomer bCus = new BCustomer();
                if (bCus.checkNRIC(sNRIC) == false)
                    bBranch.InsertCustomer(sBranch, sFullName, sNRIC, sDateBirth, sGender, sCity, sEducation, sOtherEdu);
                else
                    return "2";
                sResult = "1";
            }
            catch(Exception ex)
            {
                sResult = "0";
            }
            return sResult;
        }

        [WebMethod]
        public static string GetCustomer(int iPageNo)
        {
            int iPageSize = 3;
            BCustomer bCus = new BCustomer();
            ListCustomer lstCus = new ListCustomer();
            int iTotalRows = 0;
            try
            {
                DataSet dsCus = bCus.GetCustomer(iPageNo, iPageSize, out iTotalRows);
                if (dsCus.Tables[0].Rows.Count > 0)
                {
                    DataTable dtCus = dsCus.Tables[0];
                    lstCus.ListCus = new List<MyCustomer>();
                    int len = dtCus.Rows.Count;
                    for (int i = 0; i < len; i++)
                    {
                        DataRow dr = dtCus.Rows[i];
                        MyCustomer myCus = new MyCustomer();
                        myCus.IdCus = dr["Id"].ToString();
                        myCus.Branch = dr["BranchName"].ToString();
                        myCus.FullName = dr["FullName"].ToString();
                        myCus.NRIC = dr["NRIC"].ToString();
                        myCus.Gender = dr["Gender"].ToString();
                        myCus.Date = dr["DateBirth"].ToString();
                        myCus.City = dr["CityName"].ToString();
                        myCus.Education = dr["EduName"].ToString();
                        myCus.OtherEdu = dr["OtherEducation"].ToString();
                        lstCus.ListCus.Add(myCus);
                    }
                    lstCus.TotalRow = iTotalRows;
                    lstCus.TotalPage = GetTotalPage(iTotalRows, iPageSize);
                }
                else
                    lstCus.TotalRow = 0;
            }
            catch (Exception ex)
            {
                throw ex;
            }
            finally { 

            }

            return new JavaScriptSerializer().Serialize(lstCus);
        }

        public static int GetTotalPage(int TotalRow, int PageSize)
        {
            return TotalRow == 0 ? 0 : TotalRow / PageSize + (TotalRow % PageSize > 0 ? 1 : 0);
        }
    }

    public class MyCustomer
    {
        public string IdCus { get; set; }
        public string Branch { get; set; }
        public string FullName { get; set; }
        public string Date { get; set; }
        public string Gender { get; set; }
        public string NRIC { get; set; }
        public string City { get; set; }
        public string Education { get; set; }
        public string OtherEdu { get; set; }
    }

    [Serializable]
    public class ListCustomer
    {
        public List<MyCustomer> ListCus { get; set; }
        public int TotalRow { get; set; }
        public int TotalPage { get; set; }
    }
}